﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BulldogCMS.Core.Storage;
using System.Globalization;
using BulldogCMS.Core.Runtime;
using BulldogCMS.Core.Storage.Cache;
using System.Xml.Linq;
using System.Reflection;
using System.IO;
using BulldogCMS.Core.Storage.Providers;
using BulldogCMS.Core.Runtime.Modules;
using BulldogCMS.Core.MVC;

namespace BulldogCMS.Core
{
    public class Context
    {
        public static RequestContext Request
        {
            get
            {
                return Application.RequestContextProvider.GetRequestContext();
            }
        }

        public static ApplicationContext Application
        {
            get;
            internal set;
        }

        public static void InitApplicationContext(XDocument configurationDoc, XDocument treeDoc, string applicationPhysicalPath = null)
        {
            ConfigurationLoader configLoader = new ConfigurationLoader(configurationDoc, treeDoc);
            
            Context.Application =               new ApplicationContext();
            Context.Application.ApplicationPhysicalPath = applicationPhysicalPath;
            Context.Application.Sites =         configLoader.LoadSites();
            Context.Application.Languages =     configLoader.LoadLanguages();

            Context.Application.Assemblies =    configLoader.LoadAssemblies();
            Context.Application.Templates =     configLoader.LoadTemplates();
            Context.Application.DataTypes =     configLoader.LoadDataTypes();
            Context.Application.StorageCache =  configLoader.LoadStorageCacheProvider();
            Context.Application.UniqueIdentifierProvider =  configLoader.LoadUniqueIdentifierProvider();
            Context.Application.RequestContextProvider =    configLoader.LoadRequestContextProvider();

            Context.Application.AreaProviders = configLoader.LoadAreaModules();
            Context.Application.URLResolver =   configLoader.LoadURLResolver();

            Context.Application.Tree =          new Tree(treeDoc);
        }

        public static void CleanContext()
        {
            Context.Application =   null;
        }

        private static ResourceFilesHelper _resourceHelper = new ResourceFilesHelper();

        internal static ResourceFilesHelper ResourceFilesHelper
        {
            get
            {
                return _resourceHelper;
            }
        }

    }
}